package com.ybkj.daijia;

import com.ybkj.daijia.server.model.Brand;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

public class BrandList {

    public static List<Map<String, Object>> getBrands(List<Brand> brands) {

        if (!CollectionUtils.isEmpty(brands)) {
            List<String> brandList = new ArrayList<String>();
            for (Brand brand : brands) {
                brandList.add(brand.getName());
            }
            Collator cmp = Collator.getInstance(java.util.Locale.CHINA);
            Collections.sort(brandList, cmp);

            List<String> zimuArr = new ArrayList<String>();
            for (String brand : brandList) {
                String zimu = toPinyin(brand).substring(0, 1).toUpperCase();
                if (!zimuArr.contains(zimu)) {
                    zimuArr.add(zimu);
                }
            }

            List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
            for (String zimu : zimuArr) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("brandZimu", zimu);

                List<Brand> list = new ArrayList<Brand>();
                for (Brand brand : brands) {
                    if (toPinyin(brand.getName()).substring(0, 1).toUpperCase().equals(zimu)) {
                        list.add(brand);
                    }
                }
                map.put("brands", list);
                mapList.add(map);
            }

            return mapList;
        }

        return null;
    }

    private static String toPinyin(String zh) {

        if (StringUtils.isBlank(zh)) {
            return null;
        }

        HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
        outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

        char ch[] = zh.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ch.length; i++) {
            if (Character.toString(ch[i]).matches("[\\u4E00-\\u9FA5]+")) {
                try {
                    String[] pinyinStrArray = PinyinHelper
                        .toHanyuPinyinStringArray(ch[i], outputFormat);

                    if ((null != pinyinStrArray) && (pinyinStrArray.length > 0)) {
                        sb.append(pinyinStrArray[0]);
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                sb.append(ch[i]);
            }
        }

        return sb.toString();
    }

}  